package ncku.account.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ncku.account.data.Category;
import ncku.account.data.SubTransaction;
import ncku.account.data.SubTransactionList;

import com.toedter.calendar.JDateChooser;

public class transactionModule extends JPanel{
	//介面上的元件
	private JLabel JLDate;
    private JLabel JLDescription;
    private JLabel JLCategory;
    private JLabel JLAmount;
    protected JComboBox amountType;
    private JLabel JLSubTrans;
    protected JLabel JLTitle;
    
    //以JFormatedTextField實作輸入欄位
    protected JDateChooser JDate;
    protected JTextField InputAmount;
    protected JTextField InputDescriptionBox;
    protected categorySel categorySelection;
    
    protected subTransactionTable subTrans;
    
    protected JButton JBSubmit;
    protected JButton JBCancel;
    private JButton JBSubTransNew;
    private JButton JBSubTransDelete;
    
    //儲存新增Transaction的資料
    protected Date inputDate;
    protected String inputDescription;
    protected int inputAmount;
    protected Category inputCategory;
    public SubTransactionList subTransList = new SubTransactionList();  //新增Transaction時,就先new a subTrasanction List Object

    public transactionModule(){
    	//construct components
		JLDate = new JLabel("交易日期：");
		JLDescription = new JLabel("交易內容：");  
        JLCategory = new JLabel("交易類別");
		JLAmount = new JLabel("交易金錢");
		JLSubTrans = new JLabel("交易子細項");
		JLTitle = new JLabel("新增交易");
		JLTitle.setFont(new Font("SansSerif", Font.BOLD, 25));
		JDate = new JDateChooser("yyyy/MM/dd","####/##/##",'_');
		amountType = new JComboBox(getAmountTypeList());
		InputAmount = new JTextField (5);
		InputDescriptionBox = new JTextField(10);
		categorySelection = new categorySel(this);

		JBSubmit = new JButton("確定");
		JBCancel = new JButton("取消");
		
		subTrans = new subTransactionTable(this,subTransList);
		
		JBSubTransNew = new JButton("新增子細項");
		JBSubTransDelete = new JButton("刪除子細項");
		        
        JLDate.setVisible(true);
        JLDescription.setVisible(true);
        JLCategory.setVisible(true);
        amountType.setVisible(true);
        JLAmount.setVisible(true);
        JLSubTrans.setVisible(true);
            
        JDate.setVisible(true);
        InputAmount.setVisible(true);
        InputDescriptionBox.setVisible(true);
        categorySelection.setVisible(true);
           
        JBSubmit.setVisible(true);
        JBCancel.setVisible(true);
        JBSubTransNew.setVisible(true);
        JBSubTransDelete.setVisible(true);
        
        subTrans.setVisible(true);
        
      //adjust size and set layout
        setPreferredSize (new Dimension (681, 422));
        setLayout (null);

        //add components
        add(JLDate);
        add(JLDescription);
        add(JLCategory);
        add(amountType);
        add(JLAmount);
        add(JLTitle);
            
        add(JDate);
        add(InputAmount);
        add(InputDescriptionBox);
        add(categorySelection);
           
        add(JBSubmit);
        add(JBCancel);
        
        add(subTrans);
   
        //setBounds(50, 125, 80, 30); //(X座標,Y座標,元件寬,元件高)
        //set component bounds (only needed by Absolute Positioning)
        JLTitle.setBounds (250, 10, 200, 35);
        JLDate.setBounds (70, 70, 100, 25);
        JDate.setBounds(150,70,125,25);
        JLDescription.setBounds (70, 100, 100, 25);
        InputDescriptionBox.setBounds (150, 100, 150, 25);
        JLCategory.setBounds (360, 70, 75, 25);
        JLAmount.setBounds (360, 100, 65, 25);
        amountType.setBounds(420, 100, 65, 25);
        InputAmount.setBounds(490,100,90,25);
        categorySelection.setBounds(420, 70, 210, 30);
        
        subTrans.setBounds(50,150,550,300);

        JBSubmit.setBounds(200,380,80,30);
        JBCancel.setBounds(310,380,80,30);
    }
    
  //更新category JComboBox
    public void resetLayoutBox(){
		remove(categorySelection);
		categorySelection = new categorySel(this);
		categorySelection.setBounds(420, 70, 210, 30);
		add(categorySelection);
		revalidate();
	}
    
    //更新下方subTrans
    public void resetLayoutTable(){
    	remove(subTrans);
    	subTrans = new subTransactionTable(this,subTransList);
    	subTrans.setBounds(50,150,550,300);
    	add(subTrans);
    	revalidate();
    }
    
    public String[] getAmountTypeList(){  //取得amount type list(String array)
    	List<String> result = new ArrayList<String>();
    	String [] type = {"收入","支出"};
    	for(int i = 0; i < type.length; i++){
			result.add(type[i]);
		}
		String [] amountTypeItems = new String[result.size()];  //category list
		amountTypeItems = result.toArray(amountTypeItems);
		return amountTypeItems;
    }
    
    public int getSubTransSum(){
    	int sum = 0;
    	for(int i=0; i< subTransList.getSize(); i++){
			sum += subTransList.getSubTrans(i).getSubAmount();
		}
    	return sum;
    }
    
    public ArrayList<SubTransaction> checkSubTrans(int index, ArrayList<SubTransaction> inputSubTrans){
    	int amount;
    	for(int i=0; i<inputSubTrans.size(); i++){
    		amount = inputSubTrans.get(i).getSubAmount();
    		
    		
	    	if(index == 0){  //收入
	    		if(amount < 0){
	    			inputSubTrans.get(i).setSubAmount((amount*(-1)));   //把amount變成正數
	    		}
	    	}
	    	else{  //支出
	    		if(amount > 0){
	    			inputSubTrans.get(i).setSubAmount((amount*(-1)));   //把amount變成負數
	    		}
	    	}
    	}	
    	return inputSubTrans;
    }
    
    public int checkAmount(int index, int amount){
    	if(index == 0){  //收入
    		if(amount<0){
    			amount = amount * (-1);  //把amount變成正數
    		}
    	}
    	else{  //支出
    		if(amount>0){
    			amount = amount * (-1);  //把amount
    		}
    	}
    	return amount;
    }
}
